package app.fitness.simplefragment;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;

import utils.Utils;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import chart.AbstractDemoChart;

import com.razerzone.android.fitness.R;

public class DashBoardActiveMinsSimpleFragment extends Fragment {

	/**
	 * String section
	 */
	/**
	 * View section
	 */
	private GraphicalView mChartView;
	private LinearLayout mLl;
	public static LinearLayout mLlTimeDayMonthMonths;
	public static LinearLayout mLlTimeWeekly;
	public static LinearLayout mLlTimeYearly;
	public static TextView mTvCurrent;
	public static TextView mTvFirstDay;
	public static TextView mTvGoal;
	public static TextView mTvLastDay;
	public static TextView mTvMiddleDay;

	/**
	 * The others section
	 */

	/**
	 * Listener
	 */
	// private OnClickListener mListernerBack = new OnClickListener() {
	//
	// @Override
	// public void onClick(View v) {
	// Fragment mFm = new DashBoardFragment();
	// getFragmentManager().beginTransaction()
	// .replace(R.id.fr_content_activity_fitness, mFm)
	// .commitAllowingStateLoss();
	// getActivity().runOnUiThread(new Runnable() {
	//
	// @Override
	// public void run() {
	// ActivityFitness.mFm.executePendingTransactions();
	// }
	// });
	// }
	// };

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		/**
		 * Set Orientation for page
		 */
		getActivity().setRequestedOrientation(
				ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

		View v = null;
		if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			v = inflater.inflate(
					R.layout.simple_fragment_dashboard_active_mins, container,
					false);

			initialVariables(v);
			initialData();
		}

		return v;
	}

	/**
	 * Initialize methods
	 */

	private void initialData() {
		// Draw chart
		drawChartView();

		/**
		 * Integrate with Data to show on the page
		 */
		setFitnessData();
	}

	private void initialVariables(View v) {
		mLl = (LinearLayout) v
				.findViewById(R.id.ll_graph_in_simple_fragment_dashboard_active_mins);
		mLlTimeDayMonthMonths = (LinearLayout) v
				.findViewById(R.id.ll_time_day_month_months_in_simple_fragment_dashboard_active_mins);
		mLlTimeWeekly = (LinearLayout) v
				.findViewById(R.id.ll_time_weekly_in_dashboard_simple_fragment_active_mins);
		mLlTimeYearly = (LinearLayout) v
				.findViewById(R.id.ll_time_year_in_simple_fragment_dashboard_active_mins);
		mTvCurrent = (TextView) v
				.findViewById(R.id.tv_current_in_simple_fragment_dashboard_active_mins);
		mTvFirstDay = (TextView) v
				.findViewById(R.id.tv_first_day_in_simple_fragment_dashboard_active_mins);
		mTvGoal = (TextView) v
				.findViewById(R.id.tv_goal_in_simple_fragment_dashboard_active_mins);
		mTvLastDay = (TextView) v
				.findViewById(R.id.tv_last_day_in_simple_fragment_dashboard_active_mins);
		mTvMiddleDay = (TextView) v
				.findViewById(R.id.tv_middle_day_in_simple_fragment_dashboard_active_mins);
	}

	/**
	 * Basic methods
	 */

	private void drawChartView() {
		if (mChartView == null) {
			/**
			 * Initial Chart View
			 */
			mChartView = ChartFactory.getBarChartView(getActivity(),
					setUpDatasetChartView(), setUpRenderChartView(),
					Type.STACKED);

			/**
			 * Add Chart View
			 */
			mLl.addView(mChartView, new LayoutParams(
					android.view.ViewGroup.LayoutParams.MATCH_PARENT,
					android.view.ViewGroup.LayoutParams.MATCH_PARENT));
		} else {
			/**
			 * Remove Chart View before update Chart View again
			 */
			Utils.removeViewParent(mChartView);

			/**
			 * Reset Chart View
			 */
			mChartView = null;

			/**
			 * Initial Chart View
			 */
			mChartView = ChartFactory.getBarChartView(getActivity(),
					setUpDatasetChartView(), setUpRenderChartView(),
					Type.STACKED);

			/**
			 * Add Chart View
			 */
			mLl.addView(mChartView, new LayoutParams(
					android.view.ViewGroup.LayoutParams.MATCH_PARENT,
					android.view.ViewGroup.LayoutParams.MATCH_PARENT));
		}
	}

	private List<double[]> getChartValue() {
		List<double[]> mListValues = new ArrayList<double[]>();
		mListValues.add(new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
		return mListValues;
	}

	private XYMultipleSeriesDataset setUpDatasetChartView() {
		String[] titles = new String[] { "" };

		/**
		 * Get Values to draw on Chart
		 */
		XYMultipleSeriesDataset mDataset = AbstractDemoChart.buildBarDataset(
				titles, getChartValue());

		return mDataset;
	}

	private XYMultipleSeriesRenderer setUpRenderChartView() {
		int[] colors = new int[] { getResources().getColor(R.color.green_text) };
		XYMultipleSeriesRenderer renderer = AbstractDemoChart
				.buildBarRenderer(colors);

		renderer.setAntialiasing(true);
		renderer.setAxesColor(Color.BLACK);
		renderer.setBarSpacing(4f);
		renderer.setGridColor(Color.BLACK);
		renderer.setMarginsColor(Color.argb(0x00, 0x01, 0x01, 0x01));
		renderer.setShowGridX(true);
		renderer.setPanEnabled(false);
		renderer.setShowAxes(false);
		renderer.setShowLegend(false);
		renderer.setXLabels(0);
		renderer.setYLabels(5);
		renderer.setYLabelsColor(0, Color.TRANSPARENT);
		renderer.setZoomEnabled(false, false);

		return renderer;
	}

	private void setFitnessData() {
		// Set activity is Calories
		// mTvActivity.setText(getString(R.string.active_mins_));
	}
}
